<template>
  <div class="app-container project-evaluation-container">
    <div v-show="type == 0">
      <el-row>
        <el-col :span="24">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="计划时间">
              <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="开始时间" prop="startTime">
              <el-date-picker clearable style="width:170px" v-model="queryParams.startTime" type="date"
                value-format="yyyy-MM-dd" placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker clearable style="width:170px" v-model="queryParams.endTime" type="date"
                value-format="yyyy-MM-dd" placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item> -->
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table size="mini" height="450" :row-style="{ height: '30px' }" border :cell-style="{ padding: '0px' }"
            v-loading="loading" :data="projectInfoList" efault-expand-all @selection-change="handleSelectionChange">
            <el-table-column label="序号" type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
            <el-table-column label="项目编号" align="center" prop="projectNumber" width="105"
              :show-overflow-tooltip="true" />
            <el-table-column label="项目名称" align="center" prop="projectName" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                {{ scope.row.projectName }}
                <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
<!--                <el-button size="small" type="text"-->
<!--                  v-if="scope.row.state != 7">-->
<!--                  {{ scope.row.projectName }}-->
<!--                </el-button>-->
<!--                @click="handleClick(scope.row, scope.$index + 1)" -->
<!--                <el-button size="small" style="color: black;" type="text" v-else>-->
<!--                 -->
<!--                </el-button>-->
              </template>
            </el-table-column>
            <el-table-column label="项目负责人" align="center" prop="projectLeader" width="110"
              :show-overflow-tooltip="true" />
            <el-table-column label="计划开始时间" align="center" prop="startTime" width="95">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="计划结束时间" align="center" prop="endTime" width="95">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="项目执行人" align="center" prop="executorName" width="110"
              :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" prop="state" width="65">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.project_status" :value="scope.row.state" />
              </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="creationName" width="110" :show-overflow-tooltip="true" />
            <el-table-column label="创建时间" align="center" prop="creationTime" width="95">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="80" class-name="small-padding fixed-width">
              <template slot-scope="scope" v-if="scope.row.enterpriseId == orgId || scope.row.projectLeaderId == userId">
                <el-button v-if="scope.row.state == 6" size="mini" type="text" icon="el-icon-thumb"
                  @click="handlePJ(scope.row, scope.$index + 1)" title="评价"></el-button>
                <el-button v-if="scope.row.state == 4" size="mini" type="text" icon="el-icon-document-copy"
                  @click="handleTB(scope.row, scope.$index + 1)" title="同步"></el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-col>
      </el-row>
    </div>
    <!-- 客服评价 -->
    <div v-if="type == 2">
      <Evaluate @change="close" :opt="edit" :id="id" :sign="sign" :index="index"></Evaluate>
    </div>
    <div v-if="type == 1">
      <Operate @change="closeOpt" :opt="edit" :id="id" :index="indexSelf" :state="state" :sign="sign"></Operate>
    </div>
  </div>
</template>

<script>
import { listProjectInfo, listOver, getProjectInfo, delProjectInfo, addProjectInfo, editMainClass, updateProjectInfo } from "@/api/project/projectInfo";
import { getScheduleName } from "@/api/project/delivery";
import MainType from "@/components/MainType/indexNew";
import { getToken } from "../../../utils/auth";
import Evaluate from "../evaluation/evaluate.vue";

import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import { queryFile, fileRemove } from "@/api/system/file";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Operate from "../query/operate.vue"
export default {
  components: { MainType, Treeselect, Evaluate, Operate },
  dicts: ['project_status'],
  name: "projectSchedule",
  data() {
    return {
      userId: "",
      orgId: "",
      typeCode: 'project',
      type: 0,
      opt: 'add',
      id: '',
      sign: '',
      state: null,
      indexSelf: null,
      obj: null,
      index: null,
      edit: 'edit',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        projectName: null,
        startTime: null,
        endTime: null,

      },
      queryParamsZ: {
        type: null,

      },
      queryParamsSchedule: {
        projectId: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 日期范围
      dateRange: [],
    };
  },
  created() {
    this.getList();
    this.userId = this.$store.state.user.info.userId
    this.orgId =this.$store.state.user.info.orgId;
    console.log("this.orgId",this.orgId)
  },
  methods: {
    closeOpt() {
      this.type = 0
      this.getList();
    },
    //项目名称跳转
    handleClick(row, index) {
      this.edit = "details";
      this.indexSelf = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
      this.id = row.projectId;
      this.sign = row.sign;
      this.state = row.state;
      this.type = 1
    },
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    //树点击事件
    getNode(data, node, obj) {
      this.queryParams.classificationId = data.id
      this.getList()
    },
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      //this.queryParams.state = 6//只查询待评价状态的项目信息
      listOver(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.projectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    close(data) {
      this.type = 0
      this.getList();
      this.open = data;

    },
    /** 评价按钮操作 */
    handlePJ(row, index) {
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "pj"
      this.id = row.projectId;
      this.sign = row.sign;
      this.type = 2
    },
    /** 同步按钮操作 */
    handleTB(row, index) {

      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "tb"
      this.id = row.projectId;
      this.sign = row.sign;
      this.type = 2
    },

    /** 交付按钮操作 */
    handleJF(row, index) {
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "edit"
      this.id = row.id;
      this.sign = row.sign;
      this.type = 1
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        classificationId: null,
        projectNumber: null,
        projectName: null,
        contractId: null,
        contractName: null,
        projectLeaderId: null,
        projectLeader: null,
        executorId: null,
        executorName: null,
        startTime: null,
        endTime: null,
        attackTime: null,
        completeTime: null,
        state: null,
        operateTime: null,
        operateId: null,
        operateName: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        creationId: null,
        creationName: null,
        creationTime: null,
        belongTo: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids
      getProjectInfo(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
      });
    },
    /** 完成按钮操作 */
    handleOK(row) {
      //需要判断是否有上传交付物
      this.businessId = row.id
      this.businessCode = "project_delivery"
      //获取文件信息
      queryFile(this.businessId, this.businessCode).then(response => {
        if (response.data.length == 0) {
          this.$message.error('请先上传交付物！');
          return;
        } else {
          this.form.projectId = row.id
          this.form.state = 6
          this.$modal.confirm('是否确认完成交付项目名称为"' + row.projectName + '"的数据项？').then(function () {
          }).then(() => {
            updateProjectInfo(this.form).then(response1 => {
              this.getList();
              this.$modal.msgSuccess("操作成功");
            });
          }).catch(() => { });
        }
      });
    },
  }
};
</script>

<style scoped lang="scss">
.project-evaluation-container {
  overflow-y: auto;
  overflow-x: auto;
  padding: 16px;
  max-width: 100%;
  max-height: 90vh;
  box-sizing: border-box;
}

// ... existing styles ...
</style>
